package com.learn.api.gateway.service;

import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.gateway.event.RefreshRoutesEvent;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationEventPublisherAware;
import org.springframework.stereotype.Component;

/**
 * @author lwt<br>
 * @description <br>
 * @date 2020/8/17
 */
@Slf4j
@Component
public class RedisMessageHandler implements ApplicationEventPublisherAware {

    public static final String topic_routeDefChange = "routeDefChange";

    public static final String changeType_save = "save";

    public static final String changeType_delete = "delete";

    private ApplicationEventPublisher applicationEventPublisher;

    @Override
    public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
        this.applicationEventPublisher = applicationEventPublisher;
    }

    public void handleDefinitionChange(String message) {
        //发布 RefreshRoutesEvent 以实现路由缓存的刷新（CachingRouteLocator监听了该事件）
        log.info("received msg:"+message);
        applicationEventPublisher.publishEvent(new RefreshRoutesEvent(this));
    }
}
